
if (environment()) {
	var oMeta = document.createElement('meta');
	oMeta.httpEquiv = 'content-security-policy';
	oMeta.content = 'upgrade-insecure-requests';
	document.getElementsByTagName('head')[0].appendChild(oMeta);
}

var baseUrl2 = environment() ? "https://webv2.whoisamy.shop" : "http://test.web.chathot.me";

var $2 = (function () {
	return {
		ajax2: function ({
			type,
			url,
			data,
			isAsync,
			success,
			error,
      otherParams,
		}) {
			if (!url) {
				console.error('url错误')
				return;
			}
			var xhr = new XMLHttpRequest();
			var query = [],
				queryData;
			for (var key in data) {
				query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
			}
			queryData = query.join('&');
			if (type == 'GET') {
				if (url.indexOf('?') > -1) {
					url = url + '&' + queryData
				} else {
					url = url + '?' + queryData
				}
			}
			xhr.open(type || 'GET', url, isAsync || true);
			xhr.onreadystatechange = function () {
				if (xhr.readyState == 4 && xhr.status == 200 && success) {
					try {
						success(JSON.parse(xhr.responseText));
					} catch(err) {
						console.log(err, '---------')
						success({
							code: 400,
							data: {},
							message: 'error',
						})
					}
				} else if (xhr.readyState == 4 && xhr.status == 500) {
					error(xhr)
				}
			}
      console.log(otherParams, '打印otherParams', type == 'POST')
			if (type == 'POST' && !otherParams.formDataFlag) {
				xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
				xhr.send(queryData)
			} else if (type == 'POST' && otherParams.formDataFlag) {
				queryData = new FormData();
				for (var key in data) {
					console.log(key, data[key])
					queryData.append(key, data[key]);
				}
				xhr.send(queryData)
      } else {
				xhr.send()
			}
		}
	}
})();

const _localData = {
	loadingNum: 0,
	loadingToast: '',
	loadingTime: 300,
};

/**
 * otherParams 鍏朵粬鍙傛暟
 *  customStatus: 璧皊uccess鏃舵槸鍚﹁嚜瀹氫箟鍒ゆ柇鐘舵€佹潵鍋氬叾浠栨彁绀烘垨閫昏緫锛岄粯璁alse涓嶉渶瑕�
 *  hideLoading: true 涓峫oading
 */
function ajax2(method, param, otherParams = {}, url = '') {
	return new Promise(function (resolve, reject) {
		const getReqStart = new Date().getTime();
		$2.ajax2({
			type: method,
			url: baseUrl2 + url,
			data: param,
			success: function (data) {
				const getReqEnd = new Date().getTime();
				setTimeout(() => {
					if (!otherParams.customStatus) {
						if (handleStatus(data)) {
							reject(data)
						} else {
							resolve(data);
						}
					} else {
						resolve(data);
					}
				}, _localData.loadingTime - (getReqEnd - getReqStart))

			},
			error: function (error) {
				console.log(error)
				reject(error)
			},
      otherParams,
		});
	});
};
/*
  status: show灞曠ず鎴栬€呴殣钘廻ide
*/
function handleLoading(status) {
	if (status === 'show') {
		if (_localData.loadingNum === 0) {
			const Toast = _localData.loadingToast = Vue.prototype.$VueGlobal.Toast;
			Toast.loading({
				duration: 0,
				forbidClick: true,
			})
		}
		_localData.loadingNum += 1;
		return;
	}
	if (status === 'hide') {
		_localData.loadingNum -= 1;
		if (_localData.loadingNum === 0) {
			_localData.loadingToast && _localData.loadingToast.clear({
				clearAll: false,
			});
		}
	}
}

function handleStatus(data) {
	if (Number(data.status) == 200) return false;
	else {
		console.log(data, data.info, '---------')
		toast(data.info);
		return true;
	}
}